package com.punai.monthanalysis.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.multi.ListValueMap;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.punai.common.core.domain.BaseEntity;
import com.punai.common.utils.NumberUtil;
import com.punai.externalccb.constants.CCBConstants;
import com.punai.externalccb.domain.EcoCbhzinfo;
import com.punai.externalccb.domain.YdCbfeeiteminfoStandard;
import com.punai.externalccb.mapper.EcoCbhzinfoMapper;
import com.punai.monthanalysis.bo.ProYdcbfxDwgcrgfzldtkzbBo;
import com.punai.monthanalysis.domain.ProYdcbfxDwgcrgfzldtkzb;
import com.punai.monthanalysis.mapper.ProYdcbfxDwgcrgfzldtkzbMapper;
import com.punai.monthanalysis.service.IProYdcbfxDwgcrgfzldtkzbService;
import com.punai.monthanalysis.sqliteDao.CostStructureCategoryRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 单位工程人工费总量动态控制Service业务层处理
 *
 * @author zxw_
 * @date 2023-04-19
 */
@Service
public class ProYdcbfxDwgcrgfzldtkzbServiceImpl extends ServiceImpl<ProYdcbfxDwgcrgfzldtkzbMapper,ProYdcbfxDwgcrgfzldtkzb>
        implements IProYdcbfxDwgcrgfzldtkzbService
{
    @Autowired
    private ProYdcbfxDwgcrgfzldtkzbMapper proYdcbfxDwgcrgfzldtkzbMapper;
    @Autowired
    private CostStructureCategoryRepository costStructureCategoryRepository;
    @Autowired
    private EcoCbhzinfoMapper ecoCbhzinfoMapper;

    @Override
    public List<? extends BaseEntity> getTabsDataList(String proId,String monthId,List<YdCbfeeiteminfoStandard> list,List<YdCbfeeiteminfoStandard> categorysList) {

        List<EcoCbhzinfo> cbhzinfoList = ecoCbhzinfoMapper.selectList(Wrappers.<EcoCbhzinfo>lambdaQuery()
                .eq(EcoCbhzinfo::getProId, proId)
                .eq(EcoCbhzinfo::getYdDate, monthId)
                .eq(EcoCbhzinfo::getStagetype, CCBConstants.CCB_TYPE.YDCBFX.getCode()));
        Map<String, List<EcoCbhzinfo>> cbhzinfoMap = cbhzinfoList.stream().filter(Objects::nonNull).collect(Collectors.groupingBy(EcoCbhzinfo::getCode, Collectors.toList()));
        ListValueMap<String,YdCbfeeiteminfoStandard> listValueMap = new ListValueMap<>();
        list.forEach(ydCbfeeiteminfoStandard -> {
            listValueMap.putValue(ydCbfeeiteminfoStandard.getOpid()+"",ydCbfeeiteminfoStandard);
        });
        categorysList.forEach(ydCbfeeiteminfoStandard -> {
            listValueMap.putValue(ydCbfeeiteminfoStandard.getOpid()+"",ydCbfeeiteminfoStandard);
        });
        List<YdCbfeeiteminfoStandard> roots = listValueMap.get("0");
        List<ProYdcbfxDwgcrgfzldtkzbBo> result = new ArrayList<>();

        if (CollUtil.isNotEmpty(roots)){
            for (YdCbfeeiteminfoStandard root : roots) {
                String key = root.getName().contains("(±0.00以上)") ? "dsjz" : root.getName().contains("(±0.00以下)") ? "dxjz" : "zjz";
                ProYdcbfxDwgcrgfzldtkzbBo convert = convert(root,key,cbhzinfoMap);
                result.add(convert);
                setChildren(convert,listValueMap,key,cbhzinfoMap);
            }
        }

        return result;
    }

    private void setChildren(ProYdcbfxDwgcrgfzldtkzbBo parent,
                             ListValueMap<String, YdCbfeeiteminfoStandard> listValueMap,String key,Map<String, List<EcoCbhzinfo>> map ) {

        String id = parent.getId();
        key = parent.getItemName().contains("(±0.00以上)") ? "dsjz" : parent.getItemName().contains("(±0.00以下)") ? "dxjz" : "zjz";
        List<EcoCbhzinfo> cbhzinfoList = map.get(key);
        BigDecimal jzmj = null;
        if (CollUtil.isNotEmpty(cbhzinfoList)){
            jzmj = cbhzinfoList.stream().map(EcoCbhzinfo::getContent).filter(Objects::nonNull).map(BigDecimal::new).reduce(BigDecimal.ZERO, BigDecimal::add);
        }
        List<YdCbfeeiteminfoStandard> children = listValueMap.get(id);
        if (CollUtil.isNotEmpty(children)) {
            for (YdCbfeeiteminfoStandard child : children) {
                ProYdcbfxDwgcrgfzldtkzbBo convert = convert(child,key,map);
                parent.getChildren().add(convert);
                parent.setSjUnitHl(NumberUtil.divide(parent.getSjAllQuantity(),jzmj));
                setChildren(convert,listValueMap,key,map);
            }
        }
    }

    private ProYdcbfxDwgcrgfzldtkzbBo convert(YdCbfeeiteminfoStandard in,String key, Map<String, List<EcoCbhzinfo>> map) {
        List<EcoCbhzinfo> cbhzinfoList = map.get(key);
        BigDecimal jzmj = null;
        if (CollUtil.isNotEmpty(cbhzinfoList)){
            jzmj = cbhzinfoList.stream().map(EcoCbhzinfo::getContent).filter(Objects::nonNull).map(BigDecimal::new).reduce(BigDecimal.ZERO, BigDecimal::add);
        }

        ProYdcbfxDwgcrgfzldtkzbBo out = new ProYdcbfxDwgcrgfzldtkzbBo();
        out.setId(in.getOid()+"");
        out.setXh(in.getKeyId() + "");
        out.setItemName(in.getName());
        out.setUnit(in.getUnit());
        out.setYsAllQuantity(in.getYsAmount());
        out.setSjAllQuantity(in.getKlActualAmout());
        out.setJcs(in.getDifferenceOfCzsjAmount());
        out.setSjUnitHl(NumberUtil.divide(in.getKlActualAmout(),jzmj));
        out.setRemark("");
        return out;
    }

}
